﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.ComponentModel;

#if WINDOWS_PHONE

using Microsoft.Phone.Controls;

#endif

namespace GameLogic.Logic
{
    public class Helper
    {
        public static NavigationService GetNavigationService(FrameworkElement p)
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return null;
            }

#if WINDOWS_PHONE
            while (p != null)
            {
                if (p is PhoneApplicationPage)
                {
                    break;
                }
                p = (p as FrameworkElement).Parent as FrameworkElement;
            }

            PhoneApplicationPage pap = p as PhoneApplicationPage;
            if (pap == null)
            {
                throw new Exception("Parent must be of PhoneApplicationPage");
            }
            return pap.NavigationService;
#else
            return null;
#endif

        }


        public static NavigationContext GetNavigationContext(FrameworkElement p)
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return null;
            }

#if WINDOWS_PHONE
            while (p != null)
            {
                if (p is PhoneApplicationPage)
                {
                    break;
                }
                p = (p as FrameworkElement).Parent as FrameworkElement;
            }

            PhoneApplicationPage pap = p as PhoneApplicationPage;
            if (pap == null)
            {
                throw new Exception("Parent must be of PhoneApplicationPage");
            }
            return pap.NavigationContext;
#else
            return null;
#endif
        }


#if WINDOWS_PHONE
        internal static Microsoft.Phone.Shell.IApplicationBar GetApplicationBar(FrameworkElement p)
        {
            if (DesignerProperties.IsInDesignTool)
            {
                return null;
            }

            while (p != null)
            {
                if (p is PhoneApplicationPage)
                {
                    break;
                }
                p = (p as FrameworkElement).Parent as FrameworkElement;
            }

            PhoneApplicationPage pap = p as PhoneApplicationPage;
            if (pap == null)
            {
                throw new Exception("Parent must be of PhoneApplicationPage");
            }
            if (pap.ApplicationBar == null)
            {
                pap.ApplicationBar = new Microsoft.Phone.Shell.ApplicationBar();
            }
            return pap.ApplicationBar;
        }
#endif
    }
}
